package com.chuangmei.com.arindoorlocationsystem.utils;

import android.graphics.Color;
import android.text.Layout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目名称：MyArcGISMap
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2017/3/8 0008 下午 12:53
 * 修改人：Administrator
 * 修改时间：2017/3/8 0008 下午 12:53
 * 修改备注：
 */
public class DataUtils {
    /**
     * 保留小数位数
     *
     * @param d     格式化源数据
     * @param digit 保留位数
     * @return
     */
    public static Double DecimalD(double d, int digit) {
        if (Double.isNaN(d))
            return Double.NaN;

        //四舍五入
        d = Math.round(d * Math.pow(10, digit)) / Math.pow(10, digit);

        BigDecimal bd = new BigDecimal(d);
        bd = bd.setScale(digit, BigDecimal.ROUND_HALF_UP);
        return Double.valueOf(bd.toString());
    }

    public static float judgeStringtoFloat(String str) {
        if (TextUtils.isEmpty(str)) {
            return -1;
        }

        return Float.valueOf(str);
    }

    public static String DecimalS(double d, int digit) {
        if (Double.isNaN(d))
            return null;
        //四舍五入
        d = Math.round(d * Math.pow(10, digit)) / Math.pow(10, digit);

        String format = "";
        if (digit > 0) {
            for (int i = 0; i < digit; i++) {
                format += "0";
            }
            format = "." + format;
        }
        DecimalFormat df = new DecimalFormat("#" + format);
        String t = df.format(d);
        if (t.startsWith(".")) {
            t = "0" + t;
        }
        return t;
    }

    /**
     * 格式化日期方法
     */
    public static String DateTimeLong2String(long d) {
        Date date = new Date(d);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        return sdf2.format(date);
    }

    /**
     * int字节转成Color.rgb格式
     *
     * @param color
     * @return
     */
    public static int Int2Color(int color) {
        int red = (color & 0xff0000) >> 16;
        int green = (color & 0x00ff00) >> 8;
        int blue = (color & 0x0000ff);
        return Color.rgb(red, green, blue);
    }

    // 判断一个字符是否是中文
    private static boolean isChinese(char c) {
        return c >= 0x4E00 && c <= 0x9FA5;// 根据字节码判断
    }

    // 判断一个字符串是否含有中文
    public static boolean isHasChinese(String str) {
        if (str == null)
            return false;
        for (char c : str.toCharArray()) {
            if (isChinese(c))
                return true;// 有一个中文字符就返回
        }
        return false;
    }

    /**
     * 判断是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            System.out.println(str.charAt(i));
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }



    public static int String2Int(String v) {
        if (TextUtils.isEmpty(v)) {
            return 0;
        } else {
            return Integer.valueOf(v);
        }
    }

    public static int Object2Int(Object obj) {
        if (obj == null) {
            return 0;
        } else if (TextUtils.isEmpty(obj.toString())) {
            return 0;
        } else {
            return Integer.valueOf(obj.toString());
        }
    }

    public static Float String2Float(String v) {
        if (TextUtils.isEmpty(v)) {
            return 0f;
        } else if (v.equals(".")) {
            return 0f;
        } else if (v.startsWith(".")) {
            v = "0" + v;
            return Float.valueOf(v);
        } else if (v.endsWith(".")) {
            v = v + "0";
            return Float.valueOf(v);
        } else {
            return Float.valueOf(v);
        }
    }

    public static Double String2Double(String v) {
        if (TextUtils.isEmpty(v)) {
            return 0d;
        } else if (v.equals(".")) {
            return 0d;
        } else if (v.startsWith(".")) {
            v = "0" + v;
            return Double.valueOf(v);
        } else if (v.endsWith(".")) {
            v = v + "0";
            return Double.valueOf(v);
        } else {
            return Double.valueOf(v);
        }
    }

    /**
     * 集合转字符串,逗号分隔
     *
     * @param list
     * @return
     */
    public static String join(List<String> list) {
        String value = "";
        for (int i = 0; i < list.size(); i++) {
            if (i < list.size() - 1) {
                value += list.get(i) + ",";
            } else {
                value += list.get(i);
            }
        }
        return value;
    }

    public static String join(String div, List<String> list) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            if (i == (list.size() - 1)) {
                sb.append(list.get(i));
            } else {
                sb.append(list.get(i)).append(div);
            }
        }

        return new String(sb);
    }

    /**
     * 数组转字符串,逗号分隔
     *
     * @param list
     * @return
     */
    public static String join(String[] list) {
        String value = "";
        for (int i = 0; i < list.length; i++) {
            if (i < list.length - 1) {
                value += list[i] + ",";
            } else {
                value += list[i];
            }
        }
        return value;
    }

    public static List<String> expand(String value) {
        List<String> result = new ArrayList<String>();
        for (String t : value.split(",")) {
            result.add(t);
        }
        return result;
    }

    /**
     * 拆分集合
     *
     * @param <T>
     * @param resList 要拆分的集合
     * @param count   每个集合的元素个数
     * @return 返回拆分后的各个集合
     */
    public static <T> List<List<T>> splitList(List<T> resList, int count) {

        if (resList == null || count < 1)
            return null;
        List<List<T>> ret = new ArrayList<List<T>>();
        int size = resList.size();
        if (size <= count) { //数据量不足count指定的大小
            ret.add(resList);
        } else {
            int pre = size / count;
            int last = size % count;
            //前面pre个集合，每个大小都是count个元素
            for (int i = 0; i < pre; i++) {
                List<T> itemList = new ArrayList<T>();
                for (int j = 0; j < count; j++) {
                    itemList.add(resList.get(i * count + j));
                }
                ret.add(itemList);
            }
            //last的进行处理
            if (last > 0) {
                List<T> itemList = new ArrayList<T>();
                for (int i = 0; i < last; i++) {
                    itemList.add(resList.get(pre * count + i));
                }
                ret.add(itemList);
            }
        }
        return ret;
    }

    /**
     * 判定int数组中是否包含id
     *
     * @param data
     * @param id
     * @return
     */
    public static boolean ArrayHasInt(int[] data, int id) {
        if (data == null) {
            return false;
        }
        for (int d : data) {
            if (d == id) {
                return true;
            }
        }
        return false;
    }

    public static String toString(Object obj) {
        if (obj != null) {
            return obj.toString();
        } else {
            return null;
        }
    }

    public static String[] StringList2Array(List<String> list) {
        if (list == null) {
            return new String[]{};
        }
        String[] formulaArray = new String[list.size()];
        list.toArray(formulaArray);
        return formulaArray;
    }

    public static Integer[] IntegerList2Array(List<Integer> list) {
        if (list == null) {
            return new Integer[]{};
        }
        Integer[] formulaArray = new Integer[list.size()];
        list.toArray(formulaArray);
        return formulaArray;
    }

    public static int[] IntegerList2Array2(List<Integer> list) {
        if (list == null) {
            return new int[]{};
        }
        int[] formulaArray = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            formulaArray[i] = list.get(i);
        }
        return formulaArray;
    }

    /**
     * 获取字符串宽度
     */
    public static int getStringWidth(TextView textView) {
        int width = -1;
        TextPaint paint = textView.getPaint();
        width = (int) Layout.getDesiredWidth(textView.getText().toString(), 0,
                textView.getText().length(), paint);
        return width;
    }



    /**
     * 传入Double数组转换成去掉末尾0的String数组
     *
     * @param list
     * @return
     */
    public static String[] doubleList2StringList(double[] list) {
        String[] strList = new String[list.length];
        boolean flag = true;
        for (int i = 0; i < list.length && flag; i++) {
            double d = list[i];
            String str = String.valueOf(d);
            String[] split = str.split("\\.");
            String s = split[split.length - 1];
            char[] chars = s.toCharArray();
            for (int j = 0; j < chars.length; j++) {
                String string = String.valueOf(chars[j]);
                if (!string.equals("0")) {
                    strList[i] = str;
                    break;
                } else {
                    strList[i] = split[0];
                }
            }

        }
        return strList;
    }
}
